'''
huffman 编解码

'''

import heapq
from collections import Counter


def huffman_encode(data):
    # 统计字符频率
    frequency = Counter(data)
    # 初始化最小堆
    heap = [[weight, [char, ""]] for char, weight in frequency.items()]
    heapq.heapify(heap)
    while len(heap) > 1:
        lo = heapq.heappop(heap)
        hi = heapq.heappop(heap)
        for pair in lo[1:]:
            pair[1] = '0' + pair[1]
        for pair in hi[1:]:
            pair[1] = '1' + pair[1]
        heapq.heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
    # 生成编码表
    code_table = {char: code for char, code in heap[0][1:]}
    # 生成编码后的数据
    encoded = ''.join(code_table[char] for char in data)
    return encoded, code_table


def huffman_decode(encoded, code_table):
    reverse_table = {code: char for char, code in code_table.items()}
    decoded = ""
    current_code = ""
    for bit in encoded:
        current_code += bit
        if current_code in reverse_table:
            decoded += reverse_table[current_code]
            current_code = ""
    return decoded


# 测试
data = '''The Silent Power of Reading in the Digital Age

In an era dominated by bite-sized videos and algorithm-driven social media feeds, the act of deep reading has become a quiet rebellion against the fragmentation of attention. Unlike the passive consumption of digital content, reading demands patience, focus, and imagination—a cognitive workout that reshapes our brains in profound ways. Studies in neuroscience reveal that reading fiction enhances empathy by allowing us to "live" multiple lives through characters, while non-fiction sharpens critical thinking by exposing us to structured arguments.

Yet, the very technology that threatens to eclipse reading also offers tools to revive it. E-readers and audiobooks have democratized access to literature, breaking barriers of cost and physical storage. Apps like Goodreads create communities where solitary readers connect over shared stories, proving that reading need not be lonely. Even the much-maligned smartphone can be a portal to libraries worldwide, provided we resist the siren call of endless scrolling.

The challenge lies not in rejecting modern tools but in reclaiming intentionality. A printed book’s tactile pages or an e-reader’s glare-free screen are merely vessels; the magic unfolds in the dialogue between text and mind. As author Neil Gaiman observed, "A book is a dream you hold in your hands." In a world racing toward efficiency, choosing to read is an act of resistance—a declaration that some dreams are worth slowing down for.

Perhaps the most subversive truth is this: Reading cultivates the very skills the digital age erodes—sustained focus, nuanced understanding, and the ability to sit with complexity. In a single chapter, we might encounter ideas that took centuries to form, untangled at our own pace. That is reading’s silent superpower: It makes us time travelers, thinkers, and, ultimately, more human.”
'''
encoded_data, code_table = huffman_encode(data)
print("原始数据:", data)
print("编码表:", code_table)
print("编码结果:", encoded_data)
decoded_data = huffman_decode(encoded_data, code_table)
print("解码结果:", decoded_data)
print(('=' * 520 + '\n'))
print(('=' * 520 + '\n'))
print(('=' * 520 + '\n'))
print(f"是否与原始内容完全一致: {data == decoded_data}, 原始内容长度: {len(data)}, "
      f"编码表大小: {len(code_table)}, 编码后数据字节: {(len(encoded_data) + 7) // 8}")
